package com.yihen.service.impl;

import com.yihen.dto.Result;
import com.yihen.mappers.PatientMapper;
import com.yihen.pojo.Patient;
import com.yihen.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.Constants;

import java.util.ArrayList;
import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {
    @Autowired
    private PatientMapper patientMapper;

    @Override
    public Result getAllPatientByDoctorId(long id) {
        List<Patient> patients = patientMapper.queryAllPatientsByid(id);

        return new Result(patients);
    }

    @Override
    public Result getPatientById(long id) {
        Patient patient = patientMapper.queryPatientById(id);
        return new Result(patient);
    }

    @Override
    public Result getAllPatients() {
        List<Patient> patients = patientMapper.queryAllPatients();
        return new Result(patients);
    }

    @Override
    public Result addPatient(Patient patient) {
        // 添加医生对象
        try {
            int tag = patientMapper.insert(patient);
        } catch (Exception e) {
            // 添加失败，返回失败信息
            return new Result(Constants.ADDFAILED);
        }
        // 添加成功，返回成功信息
        return new Result(null, true, Constants.ADDSUCCESS);
    }

    @Override
    public Result deletePatientById(long id) {
        // 删除医生信息
        // TODO 完善
        int tag = patientMapper.deleteById(id);
        if (tag <= 0) {
            return new Result(Constants.DELETEFAILED);
        }
        return new Result(null, true, Constants.DELETESUCCESS);
    }

    @Override
    public Result updatePatient(Patient patient) {
        // 修改医生信息
        int tag = patientMapper.update (patient);
        // 修改成功，返回成功信息
        return new Result(null, true, Constants.UPDATESUCCESS);
    }

    @Override
    public Result getPatientByCondition(Patient patient) {
        List<Patient> patients = patientMapper.queryPatientsByCondition(patient);
        return new Result(patients);
    }

    @Override
    public Result deletePatientByIds(List<Long> ids) {
        // 删除医生信息
         // TODO 完善
        int tag = patientMapper.deleteByIds(ids);
        if (tag <= 0) {
            return new Result(Constants.DELETEFAILED);
        }
        return new Result(null, true, Constants.DELETESUCCESS);
    }
}
